
package guoan.service;

import guoan.core.dto.DataGridModel;
import guoan.core.dto.PageResponse;
import guoan.domain.AdvType;
import guoan.repository.AdvTypeRepository;

import java.util.List;

import org.hibernate.criterion.Order;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

@Service
@Transactional(readOnly = false)
public class AdvTypeService extends PageSerivce<AdvType> {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private AdvTypeRepository advTypeRepository;

    public AdvType findByAdvTypeKey(String advTypeKey){
        // TODO Auto-generated method stub
        AdvType advType = advTypeRepository.findByAdvTypeKey(advTypeKey);
        if (advType == null) {
            advType = new AdvType();
            advType.setAdvTypeKey(advTypeKey);
            advType.setCreateTime(new DateTime());
            advType = advTypeRepository.save(advType);
        }
        return advType;
    }

    public PageResponse<AdvType> page(DataGridModel dataGridModel){
        // TODO Auto-generated method stub
        dataGridModel.setOrder("desc");
        dataGridModel.setSort("orderId");
        return super.pageResponse(advTypeRepository, dataGridModel);
    }

    public AdvType findOne(Long id){
        // TODO Auto-generated method stub
        return advTypeRepository.findOne(id);
    }

    public void save(AdvType advType){
        // TODO Auto-generated method stub
        // update
        if (advType.getId() != null) {
            AdvType oldAdvType = findOne(advType.getId());
            oldAdvType.setContent(advType.getContent());
            oldAdvType.setImgUrl(advType.getImgUrl());
            oldAdvType.setOrderId(advType.getOrderId());
            oldAdvType.setShowing(advType.getShowing());
            oldAdvType.setTitle(advType.getTitle());
            oldAdvType.setwUrl(advType.getwUrl());
            advTypeRepository.save(oldAdvType);
        } else {
            advType.setCreateTime(new DateTime());
            advTypeRepository.save(advType);
        }
    }

    public void del(Long id){
        // TODO Auto-generated method stub
        advTypeRepository.delete(id);
    }

    public List<AdvType> find(){
        // TODO Auto-generated method stub
        Sort sort = new Sort(Direction.DESC, "OrderId");
        List<AdvType> advTypes = Lists.newArrayList(advTypeRepository
                .findAll(sort));
        return advTypes;
    }

    public List<AdvType> findByShowingOrderByOrderIdDesc(long l){
        // TODO Auto-generated method stub
        return advTypeRepository.findByShowingOrderByOrderIdDesc(l);
    }
}
